package doh.logic;

import doh.util.*;
import doh.service.*;
import java.io.Serializable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

@Stateless
public class GameSession implements Serializable
{

    
    GameSessionDetails gsd;
    int wins1=0;  //number of rounds won by player1
    int wins2=0;  //number of rounds won by player2

    int round = 0; //keeps track of when round is over
    int throw1 = 0;  //player1's throw
    int throw2 = 0; //playe2's throw

    boolean endGame = false;
    Vector gameSD = new Vector();

    int roundWinner = 0;
    boolean winner = false;
    int roundNumber = 0;

    //default constructor
    public GameSession() {
    }

    public GameSession(String p1, String p2)
    {
        GamePlayer player1 = new GamePlayer(p1);
        GamePlayer player2 = new GamePlayer(p2);
        gsd = new GameSessionDetails(player1, player2);
    }

    public Vector throwObject(String player, int obj)
    {
        System.out.println("Player1: " + gsd.getPlayer1().getName() + "\nPlayer2: " + gsd.getPlayer2().getName());
        System.out.println(player + " is inside throwObject");
      
        round++;

        gameSD.removeAllElements();
        
        
        if(obj == 1)
        {
            if(player.equals( gsd.getPlayer1().getName() )) 
            {
                throw1 = obj;
                gsd.getPlayer1().incRock();
            }
            else 
            {
                throw2 = obj;
                gsd.getPlayer2().incRock();
            }
        }

        else if(obj == 2)
        {
            if(player.equals( gsd.getPlayer1().getName() ))
            {
                throw1 = obj;
                gsd.getPlayer1().incPaper();
            }
            else
            {
                throw2 = obj;
                gsd.getPlayer2().incPaper();
            }
        }

        else if(obj == 3)
        {
            if(player.equals( gsd.getPlayer1().getName() ))
            {
                throw1 = obj;
                gsd.getPlayer1().incScissor();
            }
            else
            {
                throw2 = obj;
                gsd.getPlayer2().incScissor();
            }
        }
        else System.out.println("THROW NOT FOUND!!!!!!!!!!!!!!");

        System.out.println("inside throw before round==2");

        if(round == 2)
        {
            if( (throw1 == 1 && throw2 == 3) || (throw1 == 2 && throw2 == 1) || (throw1 == 3 && throw2 == 2) )
            {
                roundWinner = 1;
                wins1++;
            }
            else if((throw2 == 1 && throw1 == 3) || (throw2 == 2 && throw1 == 1) || (throw2 == 3 && throw1 == 2))
            {
                roundWinner = 2;
                wins2++;
            }
            else if(throw1 == throw2) roundWinner = 3; //if tie, do something

            
        }

        if(wins1 == 2)
        {
            winner = true;
            gsd.changeWinner(true);
        }
        else if(wins2 == 2) 
        {
            winner = true;
            gsd.changeWinner(false);
        }

        while(round<2)
        {
            if(round==2) round = 2;
            else System.out.println("Round = " + round);
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(GameSession.class.getName()).log(Level.SEVERE, null, ex);
        }

        roundNumber = wins1+wins2+1;
        gameSD.add(roundNumber);  //round number
        gameSD.add(wins1);  //number of rounds won by player 1
        gameSD.add(wins2); //number of rounds won by player2
        gameSD.add(throw1); //current throw of player1
        gameSD.add(throw2); //current throw of player2
        gameSD.add(roundWinner); //if 1: throw1 beats throw2
                                 //if 2: throw2 beats throw1
                                 //if 3: TIE!  Throw again!
    
        if(winner==true)
        {
            gameSD.add(true);
            gameSD.add(gsd);
        }
        else if(winner==false)
        {
            gameSD.add(false);
        }

        for(int i=0; i<gameSD.size(); i++)
        {
            System.out.println("Element: " + gameSD.get(i));
        }

        round=0;


        return gameSD;
    }
}
